<?php

/*
 * 
 * this is MAPS.php
 * part of the dynasties2 project on www.dixieandtheninjas.net
 * 
 * created 6/14/12
 * 
 */

session_start();


/*
$red = ImageColorAllocate($img, 255, 0, 0); 
$green = ImageColorAllocate($img, 0, 255, 0);
$sea_blue = ImageColorAllocate($img, 135, 206, 250);
$honeydew = ImageColorAllocate($img, 240, 255, 240);
$ivory3 = ImageColorAllocate($img, 205, 205, 193);
$peachpuff = ImageColorAllocate($img, 255, 218, 185);
$mistyrose = ImageColorAllocate($img, 255, 228, 225);
$dodgerblue= ImageColorAllocate($img, 30, 144, 225);
*/


class Maps extends CI_Controller {

	public function __construct() {
            parent::__construct(); 

            $this->load->helper(array('form','url', 'html'));
            $this->load->library('form_validation');
            
	}
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function index() {
            
            $this->load->view('navigation_bar_view');
            $this->load->view('MAPS_primary_view');
            //$this->load->view('MAPS_navigation_bar_view');
            
            
            $data['mydata'][] = 'My Regions';
            $data['mydata'][] = 'All Claims';
            $data['mydata'][] = 'Lords and Vassals';
            $data['mydata'][] = 'All Wars';
            $data['mydata'][] = 'All Duchies';
            $data['mydata'][] = 'Numbered Map';
            $data['mydata'][] = 'XY Coords Test';
            $data['mydata'][] = 'My Neighbors';
            $data['mydata'][] = 'My Duchy Neighbors';
            
            
            
            $this->load->view('MAPS_menu_select_view', $data);
            
        } /// end public function index()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function select_capture() {
            
            $which_map = $this->input->post('whichMap');
            
            //echo $which_map;
            
            switch ($which_map) {
                case 0:
                    //echo "something";
                    $this->display_my_region();
                    break;
                case 1:
                    $this->display_allclaims();
                    break;
                case 2:
                    $this->display_lordsandvassals();
                    break;
                case 3:
                    $this->display_allwars();
                    break;
                case 4:
                    $this->display_allduchies();
                    break;
                case 5:
                    $this->display_allnumbers();
                    break;
                case 6:
                    $this->display_circlecoords();
                    break;
                case 7:
                    $this->display_my_neighbors();
                    break;
                case 8:
                    $this->display_my_duchy_neighbors();
                    break;
                
            
            } /// end of switch $which_map
            
        } /// end public function generate_map_menu()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function display_my_region() {
            
            //$this->load->view('MAPS_navigation_bar_view');
            //$this->load->view('MAPS_myregion_view');
            
            
            
            //$this->load->view('MAPS_navigation_bar_view');
            $this->index();
            $data['mapdata'] = "maps/map_myregion";
            $this->load->view('MAPS_portable_view', $data);
            
            
        } /// end public function display_my_region()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function map_myregion() {
            
            
            //// map code ////////////////////////////////////////////
            
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                //$border = imagecolorallocate($img, 0, 0, 0);
                $border = ImageColorAt ($img, 51, 67);
                
                $dimgray = ImageColorAllocate($img,105,105,105);
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                
                $character_id = $_SESSION["active_character"];
                $title_data = $this->doctrine->em->getRepository('ORM\Dynasties2\Titles')->findBy(array('characterLink' => $character_id));
                
                foreach ($title_data as $row) {
                    
                    $coordx = $row -> getRegionLink()->getCoordX();
                    $coordy = $row -> getRegionLink()->getCoordY();
                    
                    ImageFillToBorder($img, $coordx, $coordy, $border, $dimgray);
                    
                }
                   
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
            } else {
                echo "<br>No image loaded.</br>";
            }
            
            //////////// end map code ///////////////////////////////
           
            
        }   ////// end of function map_myregion //////////////////////
        ////////////////////////////////////////////////
        
        public function display_lordsandvassals() {
            //$this->load->helper('html');
            
            //$this->load->view('MAPS_navigation_bar_view');
            $this->index();
            $data['mapdata'] = "maps/map_lordsandvassals";
            $this->load->view('MAPS_portable_view', $data);
            
            
        } /// end public function display_my_region()
        
        public function map_lordsandvassals() {
            
            
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                //$border = imagecolorallocate($img, 0, 0, 0);
                $border = ImageColorAt ($img, 51, 67);
                
                $red = ImageColorAllocate($img, 255, 0, 0); 
                $green = ImageColorAllocate($img, 0, 255, 0);
                $honeydew = ImageColorAllocate($img, 240, 255, 240);
                $ivory3 = ImageColorAllocate($img, 205, 205, 193);
                $peachpuff = ImageColorAllocate($img, 255, 218, 185);
                $mistyrose = ImageColorAllocate($img, 255, 228, 225);
                $dodgerblue= ImageColorAllocate($img, 30, 144, 225);
                
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                $color1 = mt_rand(0,255);
                $color2 = mt_rand(0,255);
                $color3 = mt_rand(0,255);
                $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                
                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Duchyregions u 
                            ");

                $duchies = $query->getResult();
                
                foreach ($duchies as $row) {
                    
                    $region_data = $this->doctrine->em->find('ORM\Dynasties2\Regions', $row->getWhichRegion());
                    
                    $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                    $coordx = $region_data->getCoordX();
                    $coordy = $region_data->getCoordY();

                    ImageFillToBorder($img, $coordx, $coordy, $border, $peachpuff);
                    
                }
                
                
                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Titles u 
                            WHERE u.vassalTo IS NOT NULL
                            ");

                $titles = $query->getResult();
                
                
                foreach ($titles as $row) {
                    
                        $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                        $coordx = $row -> getRegionLink()->getCoordX();
                        $coordy = $row -> getRegionLink()->getCoordY();

                        ImageFillToBorder($img, $coordx, $coordy, $border, $dodgerblue);
                    
                    
                }
                
                  
                
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
            } else {
                echo "<br>No image loaded.</br>";
            }
            
            
        } /// end public function lordsandvassals()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function display_allclaims() {
            
            $this->index();
            $data['mapdata'] = "maps/map_allclaims";
            $this->load->view('MAPS_portable_view', $data);
            
            
        } /// end public function lordsandvassals()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function map_allclaims() {
            
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                //$border = imagecolorallocate($img, 0, 0, 0);
                $border = ImageColorAt ($img, 51, 67);
                
                $dimgray = ImageColorAllocate($img,105,105,105);
                
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                
                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Claims u 
                        WHERE u.pending != '1'
                            ");

                $claims = $query->getResult();
                
                foreach ($claims as $row) {
                    
                    $coordx = $row -> getTitleLink()->getRegionLink()->getCoordX();
                    $coordy = $row -> getTitleLink()->getRegionLink()->getCoordY();

                    ImageFillToBorder($img, $coordx, $coordy, $border, $dimgray);
                    
                }
                
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
            } else {
                echo "<br>No image loaded.</br>";
            }
            
        } /// end public function lordsandvassals()
////////////////////////////////////////////////////////////////////////////////////////////////
       
        public function display_allwars() {
            
            $this->index();
            $data['mapdata'] = "maps/map_allwars";
            $this->load->view('MAPS_portable_view', $data);
            
        }/// end public function display_allwars()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function map_allwars() {
           
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                //$border = imagecolorallocate($img, 0, 0, 0);
                $border = ImageColorAt ($img, 51, 67);
                
                $firebrick = ImageColorAllocate($img, 178, 34, 34);
                $dodgerblue= ImageColorAllocate($img, 30, 144, 225);
                $dimgray = ImageColorAllocate($img,105,105,105);
                $orange1 = ImageColorAllocate($img,255,165,0);
                
                
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                
                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Wars u 
                        
                            ");

                $wars = $query->getResult();
                
                
                $allcombatants = array();
                
                
                foreach ($wars as $row) {
                    
                    $attacker = $row -> getAttacker();
                    $defender = $row -> getDefender();
                    
                    $allattackers[] = $attacker;
                    $alldefenders[] = $defender;
                    
                }
                
                foreach ($allattackers as $row) {
                
                    // get their regions from TITLES
                    $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Titles u 
                        WHERE u.characterLink = $row
                        ");

                    $unique_rulers = $query->getResult();

                    foreach ($unique_rulers as $row2) {

                        //echo $row2->getRegionLink()->getId();
                        ///// new code loaded, not yet working, go back further.

                    // put display code here:

                        $coordx = $row2 -> getRegionLink()->getCoordX();
                        $coordy = $row2 -> getRegionLink()->getCoordY();

                        ImageFillToBorder($img, $coordx, $coordy, $border, $firebrick);

                    }
                    
                } /// end foreach allattackers
                foreach ($alldefenders as $row) {
                
                    // get their regions from TITLES
                    $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Titles u 
                        WHERE u.characterLink = $row
                        ");

                    $unique_rulers = $query->getResult();


                    foreach ($unique_rulers as $row2) {

                        //echo $row2->getRegionLink()->getId();
                        ///// new code loaded, not yet working, go back further.

                    // put display code here:

                        $coordx = $row2 -> getRegionLink()->getCoordX();
                        $coordy = $row2 -> getRegionLink()->getCoordY();

                        ImageFillToBorder($img, $coordx, $coordy, $border, $dodgerblue);

                    }
                    
                } /// end foreach alldefenders
                
                //////////////////////////////////////////////////
                /////////////////// check to see if any attackers are also defenders!!!
                $attack_and_defend = array();
                
                foreach ($allattackers as $row) {
                    
                    //$attack_and_defend[] = array_keys($alldefenders, $row);
                    //echo "<br>Searching alldefenders for $row</br>";
                    //print_r(array_keys($alldefenders, $row));
                    //print_r($attack_and_defend);
                    if (array_keys($alldefenders, $row)) {
                        $test_array[] = array_keys($alldefenders, $row);
                        //print_r($test_array);
                    }
                    
                    
                }
                //echo "<br>******</br>";
                if (isset($test_array)) {
                    foreach ($test_array as $row) {
                        //print_r($row);
                        foreach ($row as $row2) {
                            //echo "<br>$row2 is the key to a row in alldefenders that is attacking AND defending!  " . $alldefenders[$row2];
                            //$character_data = $this->doctrine->em->find('ORM\Dynasties2\Titles', $row->getWhichRegion());
                            $titles_data = $this->doctrine->em->getRepository('ORM\Dynasties2\Titles')->findOneBy(array('characterLink' => $alldefenders[$row2]));
                            $coordx = $titles_data->getRegionLink()->getCoordX();
                            $coordy = $titles_data->getRegionLink()->getCoordY();
                            ImageFillToBorder($img, $coordx, $coordy, $border, $orange1);
                        }
                    }
                }
                
                
                /////////////////////////////////// end check to see if any attackers are also defenders
                
               
                  
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
            } else {
                echo "<br>No image loaded.</br>";
            }
            
            
        } /// end public function mapwars()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
         public function display_allduchies() {
            
            $this->index();
            $data['mapdata'] = "maps/map_allduchies";
            $this->load->view('MAPS_portable_view', $data);
            
            
        } /// end public function display_allduchies()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function map_allduchies() {
            
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                //$border = imagecolorallocate($img, 0, 0, 0);
                $border = ImageColorAt ($img, 51, 67);
                
                $dimgray = ImageColorAllocate($img,105,105,105);
                $red = ImageColorAllocate($img,255,0,0);
                
                $font = 'applicationFolder/arial.ttf';
                
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                
                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Duchyregions u 
                            ");

                $duchy_regions = $query->getResult();
                
                $duchy_data = array();
                $duchy_list = array();
                
                foreach ($duchy_regions as $row) {
                    
                    $duchy_data[$row->getWhichDuchy()][] = $row->getWhichRegion();
                    $duchy_list[] = $row->getWhichDuchy();
                    
                }
                //print_r($duchy_data);
                $duchy_list_unique = array_unique($duchy_list);
                //print_r($duchy_list_unique);
                
                foreach ($duchy_data as $key => $row) {
                    //echo $key;
                    //print_r($row);
                    
                    $color1 = mt_rand(0,255);
                    $color2 = mt_rand(0,255);
                    $color3 = mt_rand(0,255);
                    $random_color = ImageColorAllocate($img, $color1, $color2, $color3);

                    foreach ($row as $row2) {
                        //echo $row2;
                        
                        $region_data = $this->doctrine->em->find('ORM\Dynasties2\Regions', $row2);
                        
                        $coordx = $region_data->getCoordX();
                        $coordy = $region_data->getCoordY();
                        $text = $region_data->getRegionName();

                        ImageFillToBorder($img, $coordx, $coordy, $border, $random_color);
                        //imagettftext($img,9,0,$coordx,$coordy,$red,$font,$text);
                        
                    }
                }
                
                
                foreach ($duchy_list_unique as $row) {
                    
                    foreach ($duchy_data as $key => $row2) {
                        
                        if ($key == $row) {
                            
                            //echo "We got a match!</br>";
                        }
                        
                    }
                    
                }
                
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
            } else {
                echo "<br>No image loaded.</br>";
            }
            
        } /// end public function map_allduchies()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function display_allnumbers() {
            
            $this->index();
            $data['mapdata'] = "maps/map_allnumbers";
            $this->load->view('MAPS_portable_view', $data);
            
            
        } /// end public function display_allduchies()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function map_allnumbers() {
            
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                //$border = imagecolorallocate($img, 0, 0, 0);
            
                $border = ImageColorAt ($img, 51, 67);
                
                $red = ImageColorAllocate($img,255,0,0);
                $darkslategray2 = ImageColorAllocate($img,39,59,59);
                $dimgray = ImageColorAllocate($img,105,105,105);
                $font = 'applicationFolder/arial.ttf';
                
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                
                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Regions u 
                            ");

                $regions = $query->getResult();
                
                foreach ($regions as $row) {
                    
                    $coordx = $row->getCoordX();
                    $coordy = $row->getCoordY();
                    $text = $row->getRegionName();

                    //ImageFillToBorder($img, $coordx, $coordy, $border, $random_color);
                    imagettftext($img,9,0,$coordx,$coordy,$red,$font,$text);
                    
                }
                
                
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
            } else {
                echo "<br>No image loaded.</br>";
            }
            
        } /// end public function map_allduchies()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function display_circlecoords() {
        
            $this->index();
            $data['mapdata'] = "maps/map_circlecoords";
            $this->load->view('MAPS_portable_view', $data);
            
            
        } /// end public function display_allduchies()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function map_circlecoords() {
            
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                //$border = imagecolorallocate($img, 0, 0, 0);
            
                $border = ImageColorAt ($img, 51, 67);
                
                $red = ImageColorAllocate($img,255,0,0);
                $darkslategray2 = ImageColorAllocate($img,39,59,59);
                $dimgray = ImageColorAllocate($img,105,105,105);
                $font = 'applicationFolder/arial.ttf';
                
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                
                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Regions u 
                            ");

                $regions = $query->getResult();
                
                foreach ($regions as $row) {
                    
                    $coordx = $row->getCoordX();
                    $coordy = $row->getCoordY();
                    //$text = $row->getRegionName();

                    //ImageFillToBorder($img, $coordx, $coordy, $border, $random_color);
                    imagefilledellipse($img,$coordx,$coordy, 10, 10, $red);
                    
                }
                
                
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
            } else {
                echo "<br>No image loaded.</br>";
            }
            
        } /// end public function map_allduchies()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function display_my_neighbors() {
            
            //$this->load->view('MAPS_navigation_bar_view');
            //$this->load->view('MAPS_myregion_view');
            
            
            
            //$this->load->view('MAPS_navigation_bar_view');
            $this->index();
            $data['mapdata'] = "maps/map_myneighbors";
            $this->load->view('MAPS_portable_view', $data);
            
            
        } /// end public function display_my_region()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function map_myneighbors() {
            
            
            //// map code ////////////////////////////////////////////
            
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                //$border = imagecolorallocate($img, 0, 0, 0);
                $border = ImageColorAt ($img, 51, 67);
                
                $dimgray = ImageColorAllocate($img,105,105,105);
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                $peachpuff = ImageColorAllocate($img, 255, 218, 185);
                
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                
                $character_id = $_SESSION["active_character"];
                $title_data = $this->doctrine->em->getRepository('ORM\Dynasties2\Titles')->findBy(array('characterLink' => $character_id));
                
                foreach ($title_data as $row) {
                    
                    $coordx = $row -> getRegionLink()->getCoordX();
                    $coordy = $row -> getRegionLink()->getCoordY();
                    
                    ImageFillToBorder($img, $coordx, $coordy, $border, $dimgray);
                    
                } /// end foreach row (my regions)
                
                //////////////////////////////////////////////
                /////////////////////////////////////////////
                $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Titles u 
                    WHERE u.characterLink = $character_id
                        ");

                $owned_regions = $query->getResult();

                $sqlstring = "SELECT u 
                            FROM ORM\Dynasties2\Regionneighbors u ";

                if ($owned_regions) {

                    $myregions = array();
                    foreach ($owned_regions as $key => $row) {
                        $myregions[] = $row -> getRegionLink()->getId();
                        //echo $key . "- " . $row -> getRegionLink()->getId() . ", ";



                        if ($key == '0') {
                            $val = $row -> getRegionLink()->getId();
                            $sqlstring = $sqlstring . "WHERE u.whichRegion = '$val'";
                        } else {
                            $val = $row -> getRegionLink()->getId();
                            $sqlstring = $sqlstring . " OR u.whichRegion = '$val'";
                        }

                    }
                    //echo "<br>"; 


                    $query = $this->doctrine->em->createQuery("$sqlstring");

                    $neighbors = array();
                    $neighbor_data = $query->getResult();
                    foreach ($neighbor_data as $row) {
                        //echo $row->getNeighborRegion() . ", ";
                        if (!in_array($row->getNeighborRegion(), $myregions)) {
                            $neighbors[] = $row->getNeighborRegion();
                            // the above code adds regions to $neighbors() array, which is regions that are okay to claim
                            // 
                        } else {
                            //echo "<br>Removed a neighbor! " . $row->getNeighborRegion() . " test</br> ";
                        }  
                    }

                    foreach ($neighbors as $row) {
                        $region_data = $this->doctrine->em->find('ORM\Dynasties2\Regions', $row);
                        
                        $coordx = $region_data ->getCoordX();
                        $coordy = $region_data ->getCoordY();

                        ImageFillToBorder($img, $coordx, $coordy, $border, $peachpuff);
                    }

                }
                
                //////////////////////////////////////////////
                ///////////////////////////////////////////////
                   
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
            } else {
                echo "<br>No image loaded.</br>";
            }
            
            //////////// end map code ///////////////////////////////
           
            
        }   ////// end of function map_myregion //////////////////////
        ////////////////////////////////////////////////
       
        public function display_my_duchy_neighbors() {
            
            //$this->load->view('MAPS_navigation_bar_view');
            //$this->load->view('MAPS_myregion_view');
            
            
            
            //$this->load->view('MAPS_navigation_bar_view');
            $this->index();
            $data['mapdata'] = "maps/map_myduchyneighbors";
            $this->load->view('MAPS_portable_view', $data);
            
            
        } /// end public function display_my_region()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function map_myduchyneighbors() {
            
            
            //// map code ////////////////////////////////////////////
            
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                //$border = imagecolorallocate($img, 0, 0, 0);
                $border = ImageColorAt ($img, 51, 67);
                
                $dimgray = ImageColorAllocate($img,105,105,105);
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                $peachpuff = ImageColorAllocate($img, 255, 218, 185);
                
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                
                $character_id = $_SESSION["active_character"];
                
                
                //////////////////////////////////////////////
                /////////////////////////////////////////////
                $duchy = $this->doctrine->em->getRepository('ORM\Dynasties2\Duchies')->findOneBy(array('characterLink' => $character_id));
                if ($duchy) {
                    
                    $duchy_id = $duchy->getId();
                    
                     $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Duchyregions u 
                        WHERE u.whichDuchy = $duchy_id
                            ");

                    $duchy_regions = $query->getResult();
                    
                    foreach ($duchy_regions as $row) {
                        //echo "test!";
                        //echo $row;
                        $regiondata = $this->doctrine->em->find('ORM\Dynasties2\Regions', $row->getWhichRegion());
                        $coordx = $regiondata->getCoordX();
                        $coordy = $regiondata->getCoordY();

                        ImageFillToBorder($img, $coordx, $coordy, $border, $dimgray);
                        
                    }
                    
                    //////////////////////////////////////////////
                /////////////////////////////////////////////
                
                $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Titles u 
                    WHERE u.characterLink = $character_id
                        ");

                $owned_regions = $query->getResult();
                
                $owned_regions = $duchy_regions;

                $sqlstring = "SELECT u 
                            FROM ORM\Dynasties2\Regionneighbors u ";

                if ($owned_regions) {

                    $myregions = array();
                    foreach ($owned_regions as $key => $row) {
                        $myregions[] = $row -> getWhichRegion();
                        //echo $key . "- " . $row -> getRegionLink()->getId() . ", ";



                        if ($key == '0') {
                            $val = $row -> getWhichRegion();
                            $sqlstring = $sqlstring . "WHERE u.whichRegion = '$val'";
                        } else {
                            $val = $row -> getWhichRegion();
                            $sqlstring = $sqlstring . " OR u.whichRegion = '$val'";
                        }

                    }
                    //echo "<br>"; 


                    $query = $this->doctrine->em->createQuery("$sqlstring");

                    $neighbors = array();
                    $neighbor_data = $query->getResult();
                    foreach ($neighbor_data as $row) {
                        //echo $row->getNeighborRegion() . ", ";
                        if (!in_array($row->getNeighborRegion(), $myregions)) {
                            $neighbors[] = $row->getNeighborRegion();
                            // the above code adds regions to $neighbors() array, which is regions that are okay to claim
                            // 
                        } else {
                            //echo "<br>Removed a neighbor! " . $row->getNeighborRegion() . " test</br> ";
                        }  
                    }

                    foreach ($neighbors as $row) {
                        $region_data = $this->doctrine->em->find('ORM\Dynasties2\Regions', $row);
                        
                        $coordx = $region_data ->getCoordX();
                        $coordy = $region_data ->getCoordY();

                        ImageFillToBorder($img, $coordx, $coordy, $border, $peachpuff);
                    }

                }
                
                //////////////////////////////////////////////
                ///////////////////////////////////////////////
                   
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
                    
                } else {
                    
                }
                
                ////////////////////////////////////////////////////
                
                
                
                
                
            } else {
                echo "<br>No image loaded.</br>";
            }
            
            //////////// end map code ///////////////////////////////
           
            
        }   ////// end of function map_myregion //////////////////////
        ////////////////////////////////////////////////
        
} //////// end class

        
